package com.objectdraw.server.commands;

import java.io.IOException;
import java.net.Socket;

//import com.objectdraw.server.ObjectDrawServer;
import com.objectdraw.server.ObjectDrawServer;
import com.objectdraw.server.data.RequestData;
import com.objectdraw.server.data.ResponseData;

/**
 * Processes a disconnect command from the client.
 * 
 * @author jgulik, cfruehan, Harrison
 *
 */
public class DisconnectCommand extends Command {
	private static String myCommand = "disconnect";
	
	@Override
	/**
	 * Takes a message with a disconnect message and attempts to remove the client from 
	 * the list of clients and close the socket associted with said client.
	 */
	public void processRequest(RequestData request, ResponseData response) {

		if(myCommand.equalsIgnoreCase(request.getCommand())){
			System.out.println(myCommand+" Command");
			ObjectDrawServer obj = ObjectDrawServer.getInstance();
			String uname = request.getParams();
			System.out.println("Server: disconnecting "+uname);
			try {
				//TODO: figure out why removeClient isn't working!
				Socket s = obj.removeClient(uname);
				if(s != null){
					s.close();
					System.out.println("Closed connection");
				}
				request.getClient().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//Pass the request on to the next person in line
		if(this.successor != null){
			this.successor.processRequest(request,response);
		}
	}

}
